package com.dq.controller.api.staff;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ServiceProjectCategoryQuery;
import com.dq.controller.in.ServiceAdd;
import com.dq.vo.Vo;
import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.org.Org;
import com.dq.domain.record.ProfitRecord;
import com.dq.domain.record.ServiceRecord;
import com.dq.domain.service.ServiceProjectCategory;
import com.dq.domain.service.ServiceProjectRecordCentre;
import com.dq.domain.service.centre.WxUserServiceProjectCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.dq.utils.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.joda.time.DateTime;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/11/29 15:50
 */
@RestController
@RequestMapping("/api/staff/service")
@Api(description = "服务")
public class StaffUserServiceController extends AllService {


    @ApiOperation("用户信息")
    @GetMapping("getWxUserInfo/{userId}")
    public Vo getWxUserInfo(@PathVariable @ApiParam("用户id")Long userId) throws AdminException{
        SysUser user = dao.get(SysUser.class,userId);
        HashMap<String,Object> map = new HashMap<>();
        map.put("serviceNum",serviceRecordRepository.countByStaffUserAndServiceStatusAndDeletedFalse(user, ServiceRecord.ServiceStatus.endService));
        List<UserOrder.OrderSource> sources = new ArrayList<>();
        sources.add(UserOrder.OrderSource.onLine);
        sources.add(UserOrder.OrderSource.underLine);
        map.put("buyProductNum",userOrderRepository.countByStatusAndOrderSourceIn(UserOrder.UserOrderStatus.alreadyCheckOut,sources));
        BigDecimal consumeAll = profitRecordRepository.sumProfitRecordAdd(ProfitRecord.ProfitChangeType.add, ProfitRecord.ChangeProfitType.consume,user);
        map.put("consumeAll",consumeAll);
        BigDecimal profitReturn = consumeAll.subtract(profitRecordRepository.sumProfitRecordAdd(ProfitRecord.ProfitChangeType.less, ProfitRecord.ChangeProfitType.consume,user));
        map.put("consumeBalance",profitReturn);
        return new Vo(map);
    }

    @ApiOperation("开始服务")
    @PostMapping("addService")
    @Transactional(rollbackFor = Exception.class)
    public Vo addService(@RequestBody ServiceAdd serviceAdd) throws AdminException{
        SysUser user = shiroService.getUser();
        WxUser wxUser = new WxUser();
        if(serviceAdd.getWxUserId() != null){
            wxUser = dao.get(WxUser.class,serviceAdd.getWxUserId());
        }
        Subscribe subscribe = new Subscribe();
        if(serviceAdd.getFlag() == 1){
            //修改预约状态
            subscribe = dao.get(Subscribe.class,serviceAdd.getSubscribeId());
            subscribe.setSubscribeStatus(Subscribe.SubscribeStatus.inservice);
            subscribeRepository.save(subscribe);
        }
        //预约列表点开始服务 生成一条服务记录
        ServiceRecord serviceRecord = new ServiceRecord();
        serviceRecord.setServiceNo(AbstractEntity.generyStrAndMstime(""));
        serviceRecord.setWxUser(subscribe.getWxUser());
        serviceRecord.setSysDept((Org) user.getSection());
        serviceRecord.setStaffUser(user);
        serviceRecord.setServiceBeginTime(subscribe.getBeginTime());
        serviceRecord.setServiceStatus(ServiceRecord.ServiceStatus.inService);
        serviceRecord.setSubscribe(subscribe);
        serviceRecordRepository.save(serviceRecord);
        //修改床位
        staffUserService.editBedNum(user,true);
        return new Vo(HttpStatus.OK);
    }


    @ApiOperation("结束服务")
    @PostMapping("endShopService")
    @Transactional(rollbackFor = Exception.class)
    public Vo endShopService(@RequestBody ServiceAdd serviceAdd)throws AdminException{
        SysUser sysUser = shiroService.getUser();
        if(sysUser==null){
            throw new AdminException("登陆用户异常");
        }
        if(sysUser.getSection() == null){
            throw new AdminException("登录用户门店信息异常");
        }
        //返回床位数
        staffUserService.editBedNum(sysUser,false);

        if(serviceAdd.getServiceId() == null){
            throw new AdminException("请选择服务");
        }
        ServiceRecord serviceRecord = dao.get(ServiceRecord.class,serviceAdd.getServiceId());
        Subscribe subscribe = new Subscribe();
        if(serviceRecord.getSubscribe() != null){
            subscribe = dao.get(Subscribe.class,serviceRecord.getSubscribe().getId());
            subscribe.setSubscribeStatus(Subscribe.SubscribeStatus.serviced);
            subscribe = subscribeRepository.save(subscribe);
        }
        serviceRecord.setTimeoutTime(TimeUtil.getTime(TimeUtil.dayAddFMT_6(DateTime.now(),1)));
        serviceRecordService.addService(subscribe,serviceAdd,serviceRecord);
        serviceRecordRepository.save(serviceRecord);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("获取所有服务项目类别 树形结构， 或根据 服务项目 获取类别")
    @PostMapping("/list")
    public Vo list(@RequestBody ServiceProjectCategoryQuery s) throws AdminException {
        List<ServiceProjectCategory.Vo> vos = serviceProjectCategoryService.listByPage(s);

        PageHelper.startPage(s.getPage(), s.getSize());
        PageInfo pageInfo = new PageInfo(vos);
        pageInfo.setTotal(vos.size());
        return new Vo(pageInfo);
    }


}
